# -*- encoding: utf-8 -*-
from django.db import models
from utilidades import *
from django.core.urlresolvers import reverse


class Localidad(models.Model):
    localidad_id = models.AutoField(primary_key=True)
    nombre = models.CharField(max_length=50)
    class Meta:
        db_table = u'Localidad'
        verbose_name_plural = "Localidades"
        ordering = ["-nombre"]
    def __unicode__(self):
        return u'%s' % (self.nombre)

class Provincia(models.Model):
    provincia_id = models.AutoField(primary_key=True)
    nombre = models.CharField(max_length=50)
    class Meta:
        db_table = u'Provincia'
        verbose_name_plural = "Provincias"
    def __unicode__(self):
        return u'%s' % (self.nombre)

class Pais(models.Model):
    pais_id = models.AutoField(primary_key=True)
    nombre = models.CharField(max_length=50)
    class Meta:
        db_table = u'Pais'
        verbose_name_plural = "Paises"
    def __unicode__(self):
        return u'%s' % (self.nombre)

class Bombero(models.Model):
    bombero_id = models.AutoField(primary_key=True)
    apellido = models.CharField(max_length=100)
    nombre = models.CharField(max_length=100)
    direccion = models.CharField(max_length=100, blank=True)
    localidad = models.ForeignKey(Localidad, verbose_name = 'Localidad',related_name='+')
    provincia = models.ForeignKey(Provincia, verbose_name = 'Provincia',related_name='+')
    pais = models.ForeignKey(Pais, verbose_name = 'Pais',related_name='+')
    tipodoc = models.CharField('Tipo Documento',max_length=10,choices=TIPO_DOC,default=1)
    nrodoc = models.CharField('Número Documento',max_length=20, db_column=u'nroDoc') # Field name made lowercase.
    fecha_nac = models.DateField()
    lugar_nac = models.CharField(max_length=100, blank=True)
    estado_civil = models.CharField('Estado Civil',max_length=10,choices=TIPO_ESTADO_CIVIL,default=1)
    grupo_sanguineo = models.CharField('Grupo Sanguíneo',max_length=10,choices=TIPO_GS,default=1,blank=False)
    telefono = models.CharField(max_length=45, blank=True)
    celular = models.CharField(max_length=45, blank=True)
    email = models.EmailField(max_length=45, blank=True)
    observaciones = models.TextField(blank=True)
    foto= models.ImageField(blank=True, upload_to='imagenes')
    es_cadete = models.BooleanField(default=False)
    baja = models.BooleanField(default=False) # This field type is a guess.
    class Meta:
        db_table = u'Bombero'
        verbose_name_plural = "Bomberos"


    def get_absolute_url(self):
    	return reverse('bombero_detail', args=[self.pk])

    def __unicode__(self):
        return u'%s %s - %s: %s' % (self.apellido, self.nombre,self.tipodoc,self.nrodoc)


class Asociacion(models.Model):
    asociacion_id = models.AutoField(primary_key=True)
    nro_asoc = models.CharField(max_length=10)
    nro_regional = models.CharField(max_length=10)
    direccion = models.CharField(max_length=100, blank=True)
    localidad = models.ForeignKey(Localidad, verbose_name = 'Localidad',related_name='+')
    provincia = models.ForeignKey(Provincia, verbose_name = 'Provincia',related_name='+')
    telefono1 = models.CharField(max_length=45, blank=True)
    telefono2 = models.CharField(max_length=45, blank=True)
    telefono3 = models.CharField(max_length=45, blank=True)
    email = models.EmailField(blank=True)
    fax = models.CharField(max_length=45, blank=True)
    presidente = models.ForeignKey(Bombero, verbose_name = 'Presidente',related_name='+')
    jefe = models.ForeignKey(Bombero, verbose_name = 'Jefe',related_name='+')
    subjefe = models.ForeignKey(Bombero, verbose_name = 'Subjefe',related_name='+')
    baja = models.BooleanField(default=False) # This field type is a guess.

    class Meta:
        db_table = u'Asociacion'
        verbose_name_plural = "Asociaciones"

    def get_absolute_url(self):
    	return reverse('asociacion_detail', args=[self.pk])

    def __unicode__(self):
        return u'%s - %s' % (self.nro_asoc,self.nro_regional)


class Grado(models.Model):
    grado_id = models.AutoField(primary_key=True)
    descripcion = models.CharField(max_length=50)
    class Meta:
        db_table = u'Grado'
        verbose_name_plural = "Grados"
    def __unicode__(self):
        return u'%s' % (self.descripcion)

class Cargo(models.Model):
    cargo_id = models.AutoField(primary_key=True)
    descripcion = models.CharField(max_length=50)
    class Meta:
        db_table = u'Cargo'
        verbose_name_plural = "Cargos"
    def __unicode__(self):
        return u'%s' % (self.descripcion)

class Padron(models.Model):
    padron_id = models.AutoField(primary_key=True)
    anio = models.PositiveIntegerField()
    bombero = models.ForeignKey(Bombero, verbose_name = 'Bombero Voluntario',related_name='+')
    asociacion = models.ForeignKey(Asociacion, verbose_name = 'Asociacion',related_name='+')
    credencial = models.CharField(max_length=50) #Poner un REGEX en el form
    grado =models.ForeignKey(Grado, verbose_name = 'Grado',related_name='+')
    cargo =models.ForeignKey(Cargo, verbose_name = 'Cargo',related_name='+')
    nivel = models.CharField('Nivel',max_length=10,choices=TIPO_NIVEL,default=1)#En el caso de los cadetes se le carga el nivel
    ultimo_ascenso = models.DateField(null=True, blank=True)
    fecha_ingreso = models.DateField(null=True, blank=False)
    iapos = models.BooleanField(default=False,blank=True)
    reincorporaciones = models.DateField(null=True, blank=True)
    observaciones = models.TextField(blank=True)

    class Meta:
        db_table = u'Padron'
        verbose_name_plural = "Empadronados"
    @models.permalink
    def get_absolute_url(self):
        return ('padron/', [str(self.padron_id)])

    def __unicode__(self):
        return u'%s' % (self.credencial)

class Tipo_Vehiculo(models.Model):
    vehiculo_id = models.AutoField(primary_key=True)
    descripcion = models.CharField(max_length=50)
    class Meta:
        db_table = u'Tipo_Vehiculo'
        verbose_name_plural = "Tipo_Vehiculos"
    def __unicode__(self):
        return u'%s' % (self.descripcion)

class Tipo_Carroceria(models.Model):
    carroceria_id = models.AutoField(primary_key=True)
    descripcion = models.CharField(max_length=50)
    class Meta:
        db_table = u'Tipo_Carroceria'
        verbose_name_plural = "Tipo_Carrocerias"
    def __unicode__(self):
        return u'%s' % (self.descripcion)

class Tipo_Unidad(models.Model):
    tipo_unidad_id = models.AutoField(primary_key=True)
    descripcion = models.CharField(max_length=50)
    class Meta:
        db_table = u'Tipo_Unidad'
        verbose_name_plural = "Tipo_Unidades"
    def __unicode__(self):
        return u'%s' % (self.descripcion)

class Unidad(models.Model):
    unidad_id = models.AutoField(primary_key=True)
    tipo_vehiculo = models.ForeignKey(Tipo_Vehiculo, verbose_name = 'Tipo Vehículo',related_name='+')
    tipo_carroceria = models.ForeignKey(Tipo_Carroceria, verbose_name = 'Tipo Carrocería',related_name='+')
    marca = models.CharField(verbose_name = 'Marca',max_length=20)
    anio = models.PositiveIntegerField()
    numero_motor = models.CharField(max_length=45)
    numero_chasis = models.CharField(max_length=45)
    dominio = models.CharField(max_length=10)
    litros = models.DecimalField(verbose_name = 'Litros de Agua',max_digits=9, decimal_places=2)
    disp_regional=models.BooleanField(default=False);
    tipo_unidad = models.ForeignKey(Tipo_Unidad, verbose_name = 'Tipo Unidad',related_name='+')
    observaciones = models.TextField(blank=True)
    class Meta:
        db_table = u'Unidad'
        verbose_name_plural = "Unidades"

    def __unicode__(self):
        return u'%s' % (self.dominio)

    def get_absolute_url(self):
    	return reverse('unidad_detail', args=[self.pk])


